package com.alex.code;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Arrays;
import java.util.List;

// 代码自动生成器
public class CodeGeneratorV2 {
    // 自定义参数
    private static String author = "alexzshl"; // *******************************

    private static String driverName = "com.mysql.cj.jdbc.Driver"; // *******************************
    private static String url = "jdbc:mysql://localhost:3306/test_mybatis_plus"; // *******************************
    private static String username = "root";
    private static String password = "123";
    private static DbType dbType =  DbType.MYSQL;

    private static String[] includeTables = {"user", "user_underline"}; // *******************************
    private static String versionFieldName = "version"; // *******************************
    private static String deleteFieldName = "deleted"; // *******************************
    private static List<TableFill> tableFillList = Arrays.asList(
            new TableFill("create_time_by_mbp", FieldFill.INSERT),
            new TableFill("update_time_by_mbp", FieldFill.INSERT_UPDATE));

    private static String packageParent = "com.alex"; // *******************************
    private static String packageModuleName = "blog"; // *******************************
    private static String packageEntity = "entity";
    private static String packageMapper = "mapper";

    // 可选参数
    private static boolean fileOverride = true;
    private static boolean swagger2 = true;

    private static String packageService = "service";
    private static String packageServiceImpl = "service.impl";
    private static String packageController = "controller";

    // 常驻参数
    private static final String PROJECT_PATH = "user.dir";
    private static final String SOURCES = "/src/main/java";

    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        mpg.setGlobalConfig(globalConfig());
        mpg.setDataSource(dataSourceConfig());
        mpg.setPackageInfo(packageConfig());
        mpg.setStrategy(strategyConfig());
        // 自定义配置
        // 配置模板
        mpg.execute();
    }

    // 全局配置
    private static GlobalConfig globalConfig() {
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty(PROJECT_PATH);
        gc.setOutputDir(projectPath + SOURCES);
        gc.setAuthor(author);
        gc.setOpen(false); // 生成完毕打开文件夹
        gc.setFileOverride(fileOverride); // 覆盖已有文件
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(swagger2);
        return gc;
    }

    // 数据源
    private static DataSourceConfig dataSourceConfig() {
        DataSourceConfig dsc = new DataSourceConfig();

        // dsc.setSchemaName("public");
        // dsc.setDriverName("com.mysql.jdbc.Driver");
        // dsc.setUrl("jdbc:mysql://localhost:3306/ant?useUnicode=true&useSSL=false&characterEncoding=utf8");

        dsc.setDriverName(driverName);
        dsc.setUrl(url);
        dsc.setUsername(username);
        dsc.setPassword(password);
        dsc.setDbType(dbType);
        return dsc;
    }

    // 包配置
    private static PackageConfig packageConfig() {
        PackageConfig pc = new PackageConfig();
        pc.setParent(packageParent);
        pc.setModuleName(packageModuleName);
        pc.setController(packageController);
        pc.setEntity(packageEntity);
        pc.setMapper(packageMapper);
        pc.setService(packageService);
        pc.setServiceImpl(packageServiceImpl);
        return pc;
    }

    private static StrategyConfig strategyConfig() {
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();

        strategy.setInclude(includeTables); // 指定要映射的表名

        strategy.setNaming(NamingStrategy.underline_to_camel); //
        strategy.setColumnNaming(NamingStrategy.underline_to_camel); //

        strategy.setEntityLombokModel(true); // 是否添加 lombok 注解
        strategy.setRestControllerStyle(true); // 是否生成 restController

        // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        // strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        // strategy.setSuperMapperClass();
        // strategy.setSuperEntityColumns("id");

        strategy.setControllerMappingHyphenStyle(true); // 控制器的路由映射 转换为 下划线形式
        strategy.setLogicDeleteFieldName(deleteFieldName); // 逻辑删除
        strategy.setVersionFieldName(versionFieldName); // 乐观锁
        strategy.setTableFillList(tableFillList); // 自动填充

        return strategy;
    }


}
